<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class ArticleResource extends JsonResource
{
    public function toArray($request)
    {
        if ($this->type == 1) {
            $files = explode(',', $this->files);
            $data = [
                'id' => $this->id,
                'publisher_avatar' => $this->publisher->avatar,
                'publisher_name' => $this->publisher->name,
                'member_id' => $this->publisher->member->id,
                'is_vip' => 1,
                'created_at' => date('m月d日 H:i', strtotime($this->created_at)),
                'title' => $this->title,
                'content' => $this->content,
                'images' => $this->file_type == 1 ? $files : explode(' ', $this->thum_img),
                'file_type' => $this->file_type,
                'video' => $this->video,
                'thum_img' => $this->thum_img,
                'relay_count' => $this->relay_count + $this->first_relay_count,
                'comments_count' => count($this->comments) ?: 0,
                'fav_count' => $this->fav_count + $this->first_fav_count,
                'is_fav' => auth('api')->check() ? boolval(auth('api')->user()->favoriteArticles()->find($this->id)) : 0,
                'tag_id' => $this->tag_id,
                'tag' => $this->hot_news_tag ? [
                    'tag_id'   => $this->hot_news_tag->id,
                    'tag_name' => $this->hot_news_tag->name,
                ] : (object)[]
            ];
        } else {
            $data = [
                'id' => $this->id,
                'publisher_avatar' => $this->publisher->avatar,
                'publisher_name' => $this->publisher->name,
                'member_id' => $this->publisher->member->id,
                'is_vip' => 1,
                'images' => explode(' ', $this->thum_img),
                'title' => $this->title,
                'desc' => $this->desc,
                'content' => $this->content,
                'created_at' => date('m月d日 H:i', strtotime($this->created_at)),
                'relay_count' => $this->relay_count + $this->first_relay_count,
                'comments_count' => count($this->comments) ?: 0,
                'fav_count' => $this->fav_count + $this->first_fav_count,
                'pageviews' => $this->pageviews,
                'is_fav' => auth('api')->check() ? boolval(auth('api')->user()->favoriteArticles()->find($this->id)) : 0,
                'tag_id' => $this->tag_id,
                'tag' => $this->tag ? [
                    'tag_id'   => $this->tag->id,
                    'tag_name' => $this->tag->name,
                ] : (object)[]
            ];
        }
        $data['if_hot'] = !$this->is_hot;
        $data['if_cancel_hot'] = (boolean) $this->is_hot;
        $data['if_delete'] = true;

        if ($request->id) {
            $data['comments'] = [];
            foreach ($this->all_comments as $comment) {
                if($comment->member_id) {
                    $is_author = boolval($comment->member->publisher) && $comment->member->publisher->id == $this->publish_id;
                    $is_vip = boolval($comment->member->publisher) && boolval($comment->member);
                } else {
                    $is_author = $is_vip = false;
                }
                $data['comments'][] = [
                    'id' => $comment->id,
                    'member_id' => $comment->member_id,
                    'nickname' => boolval($comment->member) ? $comment->member->nickname : $comment->nickname,
                    'avatar' => boolval($comment->member) ? $comment->member->avatar : $comment->avatar,
                    'article_id' => $comment->article_id,
                    'content' => $comment->content,
                    'fav_count' => $comment->fav_count,
                    'created_at' => date('m月d日 H:i', strtotime($comment->created_at)),
                    'is_fav' => auth('api')->check() ? boolval(auth('api')->user()->favoriteArticleComments()->find($comment->id)) : 0,
                    'is_author' => $is_author,
                    'is_vip' => $is_vip,
                    'status' => $comment->status == 1,
                ];
            }
        }
        return $data;
    }
}
